package com.kfm.base.function2;

public class Demo {

    public static void main(String[] args) {
        /*
        有 5 位同学的姓名存储在一个一维数组 names 中，他们的三科（数学，语文，英语）
        成绩，存储在一个二维数组 scores 中，请根据语文成绩降序排名并输出每个人及其各科
        成绩。
         */
        int[][] scores = {
                {1, 2, 3},
                {1, 3, 2},
                {1, 1, 1},
                {2, 1, 3},
                {3, 3, 3}
        };
        String[] names = {
            "赵", "钱", "孙", "李", "周"
        };
        sort(scores, names);

        print(scores, names);
    }

    public static void print(int[][] scores, String[] names){
        for (int i = 0; i < scores.length; i++) {
            System.out.print(names[i] + ": ");
            for (int j = 0; j < scores[i].length; j++) {
                System.out.print(scores[i][j] + "\t");
            }
            System.out.println();
        }
    }
    public static void sort(int[][] scores, String[] names){
        // 冒泡
        for (int i = 0; i < scores.length - 1; i++) {
            for (int j = 0; j < scores.length - 1 - i; j++) {
                if (scores[j][1] < scores[j + 1][1]){
                   int[] temp = scores[j];
                   scores[j] = scores[j + 1];
                   scores[j + 1] = temp;

                   String name = names[j];
                   names[j] = names[j + 1];
                   names[j + 1] = name;
                }
            }
        }
    }
}
